// pages/issue/issue.js
const app = getApp()
// const post = getApp().post
const sourceType = [['camera'], ['album'], ['camera', 'album']]
const camera = [['front'], ['back'], ['front', 'back']]

const duration = Array.apply(null, {length: 60}).map(function (n, i) {
  return i + 1
})

Page({

  /**
   * 页面的初始数据
   */
  data: {
    issue_type:'diary',
    imgList:[],   //图片列表
    img_ids:[],   //上传图片返回的id列表
    media_id:-1,
    loadCount:0,  //上传完成个数
    diary_content:'' , //日志内容
    media_name:'',
    sourceTypeIndex: 2,
    sourceType: ['拍摄', '相册', '拍摄或相册'],

    cameraIndex: 2,
    camera: ['前置', '后置', '前置或后置'],

    durationIndex: 59,
    duration: duration.map(function (t) { return t + '秒' }),

    src: ''

  },
  //发布类型
  issueTypeChoice(res){
    console.log(res)
    this.setData({
      issue_type:res.detail.value
    })
  },
  // 发布按钮
  publishTap:function(){

    if(this.data.issue_type == 'diary'){
      this.issue_diary()
    }else{
      this.issue_media()
    }

    
  },
  issue_media(){
    let url = '/member/media/'
  let data = {
    type:1,
    name:this.data.media_name,
    describe: this.data.diary_content,
    img_id: this.data.img_ids[0],
    file_id:this.data.media_id
  }
  app.post(url, data, this.diary_create)
  },
issue_diary(){
  let url = '/member/diary/'
  let data = {
    content: this.data.diary_content,
    img_ids: this.data.img_ids
  }
  app.post(url, data, this.diary_create)
},

  diary_create:function(res){
    console.log(res)
  },
  // content输入框失去焦点时触发
  diaryContentBlur:function(res){
    this.setData({
      diary_content:res.detail.value
    })
  },
  //media 名称
  mediaNameBlur(res){
    this.setData({
      media_name:res.detail.value
    })
    console.log(this.data.media_name)
  },
  // 选择图片
  ChooseImage() {
    wx.chooseImage({
      count: 6, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        const tempFilePaths = res.tempFilePaths
        this.setData({
          loadModal:true,
          toatalCount:tempFilePaths.length
        })
        for(var i in tempFilePaths){
          this.uploadFileToServer(this,tempFilePaths[i],i)
        }
        this.setData({
          loadModal:false
        })
      }
    });
  },
  
  uploadFileToServer(that,tempFilePath,i){
 //  图片长传
    wx.uploadFile({
      url: app.globalData.qiaofinn + '/system/upload_file/', //仅为示例，非真实的接口地址
      filePath: tempFilePath,
      name: 'file',
      formData: {
        'user': 'test'
      },
      success (res){
        const data = JSON.parse(res.data)
        that.data.imgList.push(tempFilePath)
        that.data.img_ids.push(data['data'])
        
        that.setData({
          imgList:that.data.imgList,
          img_ids:that.data.img_ids,
          loadCount:i
        })
      }
    })
  },

   //选择视频
   chooseVideo(){
    const that = this
    wx.chooseVideo({
      sourceType: sourceType[this.data.sourceTypeIndex],
      camera: camera[this.data.cameraIndex],
      maxDuration: duration[this.data.durationIndex],
      success(res) {
        that.setData({
          src: res.tempFilePath
        })
        that.uploadMediaToServer(that,res.tempFilePath)
      }
    })
  },
  uploadMediaToServer(that,tempFilePath){
    wx.uploadFile({
      url: app.globalData.qiaofinn + '/system/upload_file/', 
      filePath: tempFilePath,
      name: 'file',
      formData: {
        'user': 'test'
      },
      success (res){
        const data = JSON.parse(res.data)
        console.log(data.data)
        that.setData({
          media_id:data['data']
        })
      }
    })
  },
  // 查看图片
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  // 删除图片
  DelImg(e) {
    this.data.imgList.splice(e.currentTarget.dataset.index, 1);
    this.data.img_ids.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      imgList: this.data.imgList,
      img_ids:this.data.img_ids
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})